﻿<template>
  <div class="teacher-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
      <el-form :model="queryParams" ref="queryForm" labelWidth="90">
        <el-row>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item label="关键字">
              <el-input v-model="queryParams.searchKey" clearable="" placeholder="请输入模糊查询关键字"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="序号">
              <el-input v-model="queryParams.xuhao" clearable="" placeholder="请输入序号"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="校内编号">
              <el-input v-model="queryParams.code" clearable="" placeholder="请输入校内编号"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="姓名">
              <el-input v-model="queryParams.name" clearable="" placeholder="请输入姓名"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="电话">
              <el-input v-model="queryParams.tel" clearable="" placeholder="请输入电话"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="用户名">
              <el-input v-model="queryParams.userName" clearable="" placeholder="请输入用户名"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="6" class="mb10">
            <el-form-item>
              <el-button-group>
                <el-button type="primary"  icon="ele-Search" @click="handleQuery" v-auth="'teacher:page'"> 查询 </el-button>
                <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
                <el-button icon="ele-ZoomIn" @click="changeAdvanceQueryUI" v-if="!showAdvanceQueryUI"> 高级 </el-button>
                <el-button icon="ele-ZoomOut" @click="changeAdvanceQueryUI" v-if="showAdvanceQueryUI"> 隐藏 </el-button>
                
              </el-button-group>
              
              <el-button-group style="margin-left:20px">
                <el-button type="primary" icon="ele-Plus" @click="openAddTeacher" v-auth="'teacher:add'"> 新增 </el-button>
                <el-button type="primary" icon="ele-Plus" @click="openUploadDialog" v-auth="'teacher:import'"> 导入 </el-button>
                <el-button type="primary" icon="ele-Coin" @click="initTeacherUserHandle" v-auth="'teacher:initTeacherUser'"> 初始化教师账号 </el-button>
              </el-button-group>
              
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 8px">
      <el-table
				:data="tableData"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
				row-key="id"
        @selection-change="handleSelectionChange"
				border="">
        <el-table-column type="selection" width="40" />
        <el-table-column type="index" label="序号" width="55" align="center"/>
        <el-table-column prop="xuhao" label="序号" width="55" show-overflow-tooltip="" />
        <el-table-column prop="code" label="校内编号" width="80" show-overflow-tooltip="" />
        <el-table-column prop="name" label="姓名" width="100" show-overflow-tooltip="" />
        <el-table-column prop="isAtSchool" label="是否在校" width="80" show-overflow-tooltip="">
          <template #default="scope">
            <el-tag v-if="scope.row.isAtSchool"> 是 </el-tag>
            <el-tag type="danger" v-else> 否 </el-tag>
            
          </template>
          
        </el-table-column>
        <el-table-column prop="isAtBook" label="是否在籍" width="80" show-overflow-tooltip="">
          <template #default="scope">
            <el-tag v-if="scope.row.isAtBook"> 是 </el-tag>
            <el-tag type="danger" v-else> 否 </el-tag>
            
          </template>
          
        </el-table-column>
        <el-table-column prop="tel" label="手机号码" width="100" show-overflow-tooltip="" />
        <el-table-column prop="userName" label="用户名" width="100" show-overflow-tooltip="" />
        <el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('teacher:edit') || auth('teacher:delete')">
          <template #default="scope">
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditTeacher(scope.row)" v-auth="'teacher:edit'"> 编辑 </el-button>
            <el-button icon="ele-Delete" size="small" text="" type="primary" @click="delTeacher(scope.row)" v-auth="'teacher:delete'"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
				v-model:currentPage="tableParams.page"
				v-model:page-size="tableParams.pageSize"
				:total="tableParams.total"
				:page-sizes="[10, 20, 50, 100, 200, 500]"
				small=""
				background=""
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
	/>
      <printDialog
        ref="printDialogRef"
        :title="printTeacherTitle"
        @reloadTable="handleQuery" />
      <editDialog
        ref="editDialogRef"
        :title="editTeacherTitle"
        @reloadTable="handleQuery"
      />
    </el-card>
    <el-dialog v-model="state.dialogUploadVisible" :lock-scroll="false" draggable width="400px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-UploadFilled /> </el-icon>
					<span> 上传文件 </span>
				</div>
			</template>
			<div>
				<el-upload ref="uploadRef" drag :auto-upload="false" :limit="1" :file-list="state.fileList" action="" :on-change="handleChange" accept=".jpg,.png,.bmp,.gif,.txt,.pdf,.xlsx,.docx">
					<el-icon class="el-icon--upload">
						<ele-UploadFilled />
					</el-icon>
					<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
					<template #tip>
						<div class="el-upload__tip">请上传大小不超过 10MB 的文件</div>
            <el-link @click="download">导入模板下载</el-link>
					</template>
				</el-upload>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="state.dialogUploadVisible = false">取消</el-button>
					<el-button type="primary" @click="uploadFile">确定</el-button>
				</span>
			</template>
		</el-dialog>
    <el-dialog v-model="state.dialogImportVisible" :lock-scroll="false" draggable width="1000px">
			<div>
        <el-table
          :data="importData"
          style="width: 100%"
          v-loading="loading"
          tooltip-effect="light"
          border="">
          <el-table-column prop="xuhao" label="序号" width="140" show-overflow-tooltip="" />
          <el-table-column prop="code" label="校内编号" width="140" show-overflow-tooltip="" />
          <el-table-column prop="name" label="姓名" width="140" show-overflow-tooltip="" />
          <el-table-column prop="tel" label="电话" width="140" show-overflow-tooltip="" />
          <el-table-column prop="userName" label="用户名" width="140" show-overflow-tooltip="" />
          <el-table-column prop="msg" label="导入检查消息" width="140" show-overflow-tooltip="" />
        </el-table>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="state.dialogImportVisible = false">取消</el-button>
					<el-button type="primary" @click="Import">确定</el-button>
				</span>
			</template>
		</el-dialog>
  </div>
</template>

<script lang="ts" setup="" name="teacher">
  import { onMounted, reactive, ref } from 'vue';
  import { ElMessageBox, ElMessage, UploadInstance } from 'element-plus';
  import { auth } from '/@/utils/authFunction';
  import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  import { formatDate } from '/@/utils/formatTime';

  import printDialog from '/@/views/system/print/component/hiprint/preview.vue'
  import editDialog from '/@/views/main/teacher/component/editDialog.vue'
  import { pageTeacher, deleteTeacher,importPreviewTeacher,importTeacher, initTeacherUser } from '/@/api/main/teacher';

  import { SysFileApi } from '/@/api-services/api';
  import { SysFile } from '/@/api-services/models';

  import { downloadByUrl } from '/@/utils/download';

  import { getAPI } from '/@/utils/axios-utils';

  // const baseUrl = window.__env__.VITE_API_URL;
  const uploadRef = ref<UploadInstance>();
  const state = reactive({
    loading: false,
    fileData: [] as Array<SysFile>,
    queryParams: {
      fileName: undefined,
      startTime: undefined,
      endTime: undefined,
    },
    tableParams: {
      page: 1,
      pageSize: 10,
      total: 0 as any,
    },
    dialogUploadVisible: false,
    diaglogEditFile: false,
    fileList: [] as any,
    dialogDocxVisible: false,
    dialogXlsxVisible: false,
    dialogPdfVisible: false,
    dialogImportVisible: false,
    showViewer: false,
    docxUrl: '',
    excelUrl: '',
    pdfUrl: '',
    fileName: '',
    previewList: [] as string[],
  });

  const showAdvanceQueryUI = ref(false);
  const printDialogRef = ref();
  const editDialogRef = ref();
  const loading = ref(false);
  const tableData = ref<any>([]);
  const importData = ref<any>([]);
  const queryParams = ref<any>({});
  const tableParams = ref({
    page: 1,
    pageSize: 10,
    total: 0,
  });

  const checkedIds = ref<any>([])

  const printTeacherTitle = ref("");
  const editTeacherTitle = ref("");

  // 改变高级查询的控件显示状态
  const changeAdvanceQueryUI = () => {
    showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
  }
  

  // 查询操作
  const handleQuery = async () => {
    loading.value = true;
    var res = await pageTeacher(Object.assign(queryParams.value, tableParams.value));
    tableData.value = res.data.result?.items ?? [];
    tableParams.value.total = res.data.result?.total;
    loading.value = false;
  };

  // 打开新增页面
  const openAddTeacher = () => {
    editTeacherTitle.value = '添加教师';
    editDialogRef.value.openDialog({});
  };

  // 打开打印页面
  const openPrintTeacher = async (row: any) => {
    printTeacherTitle.value = '打印教师';
  }
  
  // 打开编辑页面
  const openEditTeacher = (row: any) => {
    editTeacherTitle.value = '编辑教师';
    editDialogRef.value.openDialog(row);
  };

  // 删除
  const delTeacher = (row: any) => {
    ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
  .then(async () => {
    await deleteTeacher(row);
    handleQuery();
    ElMessage.success("删除成功");
  })
  .catch(() => {});
  };

  // 改变页面容量
  const handleSizeChange = (val: number) => {
    tableParams.value.pageSize = val;
    handleQuery();
  };

  // 改变页码序号
  const handleCurrentChange = (val: number) => {
    tableParams.value.page = val;
    handleQuery();
  };

  // 打开上传页面
  const openUploadDialog = () => {
    state.fileList = [];
    state.dialogUploadVisible = true;
  };

  // 通过onChanne方法获得文件列表
  const handleChange = (file: any, fileList: []) => {
    state.fileList = fileList;
  };

  // 上传
  const uploadFile = async () => {
    if (state.fileList.length < 1) return;
    var res = await getAPI(SysFileApi).apiSysFileUploadFilePostForm(state.fileList[0].raw);
    handleQuery();
    ElMessage.success('上传成功');
    state.dialogUploadVisible = false;
    var r = await importPreviewTeacher(res.data.result?.id);
    importData.value = r.data.result ?? [];
    state.dialogImportVisible = true;
  };
  const Import= async () => {
    var r = await importTeacher(importData.value);
    if(r.status === 200){
      ElMessage.success('导入成功');
      
    }else{
      ElMessage.error(r.message);
    }
    state.dialogImportVisible = false;
    handleQuery();
  };

  const download = () => {
    let fileUrl = '/download/teacher.xlsx';////teacher.xlsx Upload
    downloadByUrl({ url: fileUrl });
  };

  const initTeacherUserHandle = async () => {
    loading.value = true;
    await initTeacherUser(checkedIds.value);
    handleQuery();
    ElMessage.success("初始化教师账号完成");
    loading.value = false;
  }

  const handleSelectionChange = (selection) => {
    checkedIds.value = selection.map((item) => item.id)
  }

  handleQuery();
</script>
<style scoped>
:deep(.el-ipnut),
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>

